package com.dayu.finecomm.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;

import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayu.finecomm.protocol.SpecialCareChannelInboundAdapter;
import com.dayu.finecomm.source.DistributeSource;

public class DistributeHandler extends
		SpecialCareChannelInboundAdapter<DatagramPacket> {

	static Logger LOGGER = LoggerFactory.getLogger(DistributeHandler.class);

	DistributeSource source;

	public DistributeHandler(DistributeSource source) {

		this.source = source;
	}

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)
			throws Exception {

		for (InetSocketAddress sa : source.getDistributors()) {

			ctx.writeAndFlush(new DatagramPacket(msg.content().retain(), sa));
		}
		ctx.fireChannelRead(msg);
	}
}
